package com.jiutong.meipengdai.ui.activitys;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jiutong.meipengdai.R;
import com.jiutong.meipengdai.base.BaseActivity;
import com.jiutong.meipengdai.entity.EvaluateResultBean;
import com.jiutong.meipengdai.http.present.CommonPresent;
import com.jiutong.meipengdai.http.subscribers.SubscriberOnNextListener;
import com.jiutong.meipengdai.utils.NumUilt;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 額度评估结果
 * Created by suxi on 2017/5/25.
 */

public class EvaluateResultActivity extends BaseActivity {


    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_account)
    TextView tvAccount;
    @Bind(R.id.tv_price_desc)
    TextView tvPriceDesc;
    @Bind(R.id.tv_location)
    TextView tvLocation;
    @Bind(R.id.tv_village)
    TextView tvVillage;
    @Bind(R.id.tv_area)
    TextView tv_area;
    @Bind(R.id.tv_orientation)
    TextView tvOrientation;
    @Bind(R.id.tv_floor)
    TextView tvFloor;
    @Bind(R.id.btn_apply_loan)
    Button btnApplyLoan;
    @Bind(R.id.btn_keepgo)
    Button btn_keepgo;
    @Bind(R.id.tv_quanliren)
    TextView tvQuanliren;
    @Bind(R.id.tv_id_num)
    TextView tvIdNum;
    @Bind(R.id.tv_nianxian)
    TextView tvNianxian;
    @Bind(R.id.tv_dengji_time)
    TextView tvDengjiTime;
    @Bind(R.id.tv_dengji_price)
    TextView tvDengjiPrice;
    @Bind(R.id.tv_zongdi_num)
    TextView tvZongdiNum;
    @Bind(R.id.tv_zhongdi_mj)
    TextView tvZhongdiMj;
    @Bind(R.id.tv_building_num)
    TextView tvBuildingNum;
    @Bind(R.id.tv_house_num)
    TextView tvHouseNum;
    @Bind(R.id.ll_more_content)
    LinearLayout llMoreContent;
    @Bind(R.id.ll_apply_des)
    LinearLayout llApplyDes;
    @Bind(R.id.ll_bottom_contaner)
    LinearLayout llBottomContaner;
    @Bind(R.id.tv_unit)
    TextView tvUnit;

    @Bind(R.id.tv_apply_money)
    TextView tvApplyMoney;
    @Bind(R.id.tv_apply_status)
    TextView tvApplyStatus;
    private String houseId;
    private boolean from_record_activity; //是否从评估记录过来
    private boolean hasApply;

    @Override
    public int getLayoutResouceId() {
        return R.layout.activity_evaluate_result;
    }

    @Override
    protected void initDate(Bundle savedInstanceState) {
        super.initDate(savedInstanceState);
        Bundle extras = getIntent().getExtras();

        houseId = extras.getString("houseId");
        from_record_activity = extras.getBoolean("from_record_activity");
        hasApply = extras.getBoolean("hasApply");
    }

    @Override
    protected void initView() {
        super.initView();
        ButterKnife.bind(this);
        tvTitle.setText("评估结果");
        if (hasApply) {

            llBottomContaner.setVisibility(View.GONE);
            llApplyDes.setVisibility(View.VISIBLE);
        }

        if (!TextUtils.isEmpty(houseId)) {
//            setContent();

            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("houseNo", houseId);
            CommonPresent.getEvaluateResult(mContext, new SubscriberOnNextListener<EvaluateResultBean>() {
                @Override
                public void onNext(EvaluateResultBean result) {

                    String totalPrice = result.getData().getTotalPrice();

                    tvPriceDesc.setText(result.getData().getAreaName() + "的合理估价为");
                    float price = Float.parseFloat(totalPrice);

                    if (price >= 10000) {
                        tvAccount.setText(NumUilt.formatFloat(price / 10000) + "");
                        tvUnit.setVisibility(View.VISIBLE);
                    } else {
                        tvAccount.setText(price + "");
                        tvUnit.setVisibility(View.GONE);
                    }

                    tvLocation.setText(result.getData().getCityName());
                    tvVillage.setText(result.getData().getAreaName());
                    tv_area.setText(result.getData().getArea() + "㎡");
                    tvBuildingNum.setText(result.getData().getBuildNo() + "栋");
                    tvHouseNum.setText(result.getData().getRoomNo() + "号");
                    tvFloor.setText("第" + result.getData().getInFloor() + "层/共" + result.getData().getTotalFloor() + "层");
                    tvOrientation.setText(result.getData().getOrientate());

                    float amountFloat = Float.parseFloat(result.getData().getApplyAmount());
                    tvApplyMoney.setText(amountFloat >= 10000 ? NumUilt.formatFloat(amountFloat / 10000) + "万" : amountFloat + "");
//                    tvApplyMoney.setText(result.getData().getApplyAmount());
                    tvApplyStatus.setText(result.getData().getLoanStatus());
                }

                @Override
                public void onError(Throwable e) {

                }
            }, hashMap);
        }
    }


    @Override
    protected void initEvent() {
        super.initEvent();
    }

    @OnClick({R.id.iv_back, R.id.btn_apply_loan, R.id.btn_keepgo})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.btn_apply_loan:

                if (isLogin()) {
                    openActivity(LoanApplyActivity.class);
                }

                break;
            case R.id.btn_keepgo:
                if (from_record_activity) {
                    openActivity(EvaluateActivity.class);
                }

                finish();
                break;

//            case R.id.tv_whach_more:
//                llMoreContent.setVisibility(View.VISIBLE);
//                tvWhachMore.setVisibility(View.GONE);
//                break;
        }
    }
}
